package com.muyu.product.service.impl;

import java.util.List;

import com.muyu.common.core.utils.ObjUtils;
import com.muyu.product.domain.AsProductAttributeInfo;
import com.muyu.product.domain.ProjectSkuInfo;
import com.muyu.product.domain.model.AttrValueModel;
import com.muyu.product.domain.model.ProjectAddModel;
import com.muyu.product.domain.model.ProjectSkuModel;
import com.muyu.product.domain.req.ProjectInfoSaveReq;
import com.muyu.product.service.AsProductAttributeInfoService;
import com.muyu.product.service.ProjectSkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.ASTProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.product.mapper.ProjectInfoMapper;
import com.muyu.product.domain.ProjectInfo;
import com.muyu.product.service.ProjectInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 商品信息Service业务层处理
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Slf4j
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo>  implements ProjectInfoService {

    @Autowired
    private AsProductAttributeInfoService asProductAttributeInfoService;

    @Autowired
    private ProjectSkuInfoService projectSkuInfoService;
    /**
     * 查询商品信息列表
     *
     * @param projectInfo 商品信息
     * @return 商品信息
     */
    @Override
    public List<ProjectInfo> list(ProjectInfo projectInfo) {
        LambdaQueryWrapper<ProjectInfo> queryWrapper = new LambdaQueryWrapper<>();


        if (ObjUtils.notNull(projectInfo.getName())){
            queryWrapper.like(ProjectInfo::getName, projectInfo.getName());
        }

        if (ObjUtils.notNull(projectInfo.getIntroduction())){
            queryWrapper.eq(ProjectInfo::getIntroduction, projectInfo.getIntroduction());
        }

        if (ObjUtils.notNull(projectInfo.getMianType())){
            queryWrapper.eq(ProjectInfo::getMianType, projectInfo.getMianType());
        }

        if (ObjUtils.notNull(projectInfo.getParentType())){
            queryWrapper.eq(ProjectInfo::getParentType, projectInfo.getParentType());
        }

        if (ObjUtils.notNull(projectInfo.getType())){
            queryWrapper.eq(ProjectInfo::getType, projectInfo.getType());
        }

        if (ObjUtils.notNull(projectInfo.getImage())){
            queryWrapper.eq(ProjectInfo::getImage, projectInfo.getImage());
        }

        if (ObjUtils.notNull(projectInfo.getCarouselImages())){
            queryWrapper.eq(ProjectInfo::getCarouselImages, projectInfo.getCarouselImages());
        }

        if (ObjUtils.notNull(projectInfo.getStatus())){
            queryWrapper.eq(ProjectInfo::getStatus, projectInfo.getStatus());
        }

        if (ObjUtils.notNull(projectInfo.getRuleId())){
            queryWrapper.eq(ProjectInfo::getRuleId, projectInfo.getRuleId());
        }

        if (ObjUtils.notNull(projectInfo.getBrandId())){
            queryWrapper.eq(ProjectInfo::getBrandId, projectInfo.getBrandId());
        }





        return list(queryWrapper);
    }

    @Override
    public boolean save(ProjectInfoSaveReq projectInfoSaveReq) {
        ProjectAddModel projectAddModel = projectInfoSaveReq.getProjectAddModel();
        ProjectInfo projectInfo = ProjectInfo.saveModelBuild(projectAddModel);
        boolean save = this.save(projectInfo);
        if (save){
            List<AttrValueModel> attrValueList = projectInfoSaveReq.getAttrValueList();
            List<AsProductAttributeInfo> asProductAttributeInfoList = attrValueList.stream()
                    //谁构建成谁 lamabda表达式
                    .map(attrValueModel ->
                            AsProductAttributeInfo.attrValueModelBuild(attrValueModel, projectInfo::getId))
                    .toList();
            if (!asProductAttributeInfoList.isEmpty()){
//                不为空
                //            批量添加 mybatisPlus**
                asProductAttributeInfoService.saveBatch(asProductAttributeInfoList);
            }

            List<ProjectSkuModel> projectSkuModelList = projectInfoSaveReq.getPrjectSkuList();
            /**
             * projectSkuModel -> {
             *                                 ProjectSkuInfo.builder()
             *                                         .projectId(projectInfo.getId())
             *                                         .sku(projectSkuModel.getSku())
             *                                         .image(projectSkuModel.getImage())
             *                                         .stock(projectSkuModel.getStock())
             *                                         .build()
             *                             }
             */
            List<ProjectSkuInfo> skuInfoList = projectSkuModelList.stream()
                    .map(
//                            lamada表达式                                                       build了
                            projectSkuModel -> ProjectSkuInfo.productSkuModelBuild(projectSkuModel, projectInfo::getId)
                    ).toList();
            if (!skuInfoList.isEmpty()){
                projectSkuInfoService.saveBatch(skuInfoList);
            }
        }

        return save;
    }
}
